package tuvienna.jade;

import jade.core.Agent;
import jade.wrapper.StaleProxyException;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Meta-Agent who will set up the other agents
 * This is a standard setup with a not too difficult set of participants
 */
public class SetupAgent extends Agent{
		
			
	private static final long serialVersionUID = 5207800529813008854L;
	


	
	protected void setup() {
	
		String[] interests1 = { "Outdoor-Halma", "Sudern" };
		String[] interests2 = { "Arbeiten", "Sport", "Kultur" };
		String[] interests3 = { "Essen", "Ancient Astronoauts","Sport" };
		String[] interests4 = { "Putzen", "Arbeiten","Trinken" };		
		String[] interests5 = { "Trinken", "Sport", "Schlafen","Musik" };
		String[] interests6 = { "Freiwillige Feuerwehr", "Lesen","Party", "Arbeiten"};
		String[] interests7 = { "Arbeiten","Musik","Lesen","Sport" };
		String[] interests8 = { "Arbeiten","Musik","Lesen","Party" };
		

		try {
			String[] eArgs = {"-evaluationIntervall","1000"};

			createNewAgent("Evaluator", "tuvienna.jade.EvaluatorAgent",eArgs);
			
			String[] tArgs  = {"-discussionTickerTime","1000","-gui","true"};
			createNewAgent("Table1", "tuvienna.jade.table.TableAgent",tArgs);
			createNewAgent("Table2", "tuvienna.jade.table.TableAgent",tArgs);
			createNewAgent("Table3", "tuvienna.jade.table.TableAgent",tArgs);
			createNewAgent("Table4", "tuvienna.jade.table.TableAgent",tArgs);
			createNewAgent("Table5", "tuvienna.jade.table.TableAgent",tArgs);
			createNewAgent("Table6", "tuvienna.jade.table.TableAgent",tArgs);
			createNewAgent("Table7", "tuvienna.jade.table.TableAgent",tArgs);
			createNewAgent("Table8", "tuvienna.jade.table.TableAgent",tArgs);
			
			String[] args  = {"-oldAgeForTable","35","-verbose","false"};
					
			for (int i=0; i<7; i++) {
				createNewAgent("A"+i, "tuvienna.jade.participant.DynamicParticipantAgent",mergeArgs(args,interests1));
				createNewAgent("B"+i, "tuvienna.jade.participant.DynamicParticipantAgent",mergeArgs(args,interests2));
				createNewAgent("C"+i, "tuvienna.jade.participant.DynamicParticipantAgent",mergeArgs(args,interests3));
				createNewAgent("D"+i, "tuvienna.jade.participant.DynamicParticipantAgent",mergeArgs(args,interests4));
				createNewAgent("E"+i, "tuvienna.jade.participant.DynamicParticipantAgent",mergeArgs(args,interests5));
				createNewAgent("F"+i, "tuvienna.jade.participant.DynamicParticipantAgent",mergeArgs(args,interests6));
				createNewAgent("G"+i, "tuvienna.jade.participant.DynamicParticipantAgent",mergeArgs(args,interests7));
				createNewAgent("H"+i, "tuvienna.jade.participant.DynamicParticipantAgent",mergeArgs(args,interests8));
			}
		
			this.doDelete();
			System.out.println("Setup complete");
		} catch (StaleProxyException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}			
		}

    protected void createNewAgent(String name, String className, Object[] args) throws StaleProxyException, InterruptedException {
		this.getContainerController().createNewAgent(name, className,  args).start();
		Thread.sleep(50);
	}
	
	protected Object[] mergeArgs(Object[] args, Object[] interests) {
		if (args == null) {
			return interests;
		} else {
			Object[] finalArgs = new Object[args.length + interests.length +1];
			int pos = 0;
			finalArgs[pos++] = "-interests";
			for (int i =0; i<interests.length; i++) {
				finalArgs[pos++] = interests[i];
			}
			for (int i = 0; i<args.length; i++) {
				finalArgs[pos++] = args[i];
			}
			return finalArgs;
		}
	}
	
	
}
